<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="targetType">
          <el-select v-model="filters.targetType" placeholder="目标类型" style="width:215px">
            <el-option
              v-for="item in targetTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="indicatorName">
          <el-select v-model="filters.indicatorName" placeholder="指标名称" style="width:215px">
            <el-option
              v-for="item in indicatorNames"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="time">
          <el-date-picker
            v-model="filters.time"
            type="daterange" value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="生效开始时间"
            end-placeholder="生效结束时间">
          </el-date-picker>
        </el-form-item>
        <el-row>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')"/>
          </el-form-item>
        </el-row>
        <el-row>
          <template>
            <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
              <el-tab-pane label="周目标" name="1"></el-tab-pane>
              <el-tab-pane label="月目标" name="2"></el-tab-pane>
              <el-tab-pane label="季目标" name="3"></el-tab-pane>
              <el-tab-pane label="年目标" name="4"></el-tab-pane>
            </el-tabs>
          </template>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      @selectionChange="selectionChange"
      @handleDelete="handleDelete"
      @handleEdit="handleEdit"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="45%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="id" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="目标类型" prop="targetType">
          <el-select v-model="dataForm.targetType"  style="width:100%">
            <el-option
              v-for="item in targetTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="指标名称" prop="indicatorName">
          <el-select v-model="dataForm.indicatorName"  style="width:100%">
            <el-option
              v-for="item in indicatorNames"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="周期类型:" prop="periodType">
          <el-select v-model="dataForm.periodType"  style="width:100%">
            <el-option
              v-for="item in periodTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生效时间" prop="time">
          <el-date-picker
            v-model="dataForm.time" style="width:100%"
            type="daterange" value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="目标值" prop="targetValue">
          <el-input v-model="dataForm.targetValue" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  //import CyTable from "@/views/Core/CyTable";
  import KtButton from "@/views/Core/KtButton";
  import {format} from "@/utils/datetime";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
  import CyTable from "@/views/Core/StaticTable";

  export default {
    components: {
      CyTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        activeName: '1',
        targetTypes: [ {
          value: '平台目标',
          label: '平台目标'
        }, {
          value: '商家目标',
          label: '商家目标'
        }],
        indicatorNames: [{
          value: '成交金额',
          label: '成交金额'
        }, {
          value: '成交客户数',
          label: '成交客户数'
        }, {
          value: '毛利',
          label: '毛利'
        }, {
          value: '新客数',
          label: '新客数'
        }, {
          value: '成交转化率',
          label: '成交转化率'
        }],
        targetTypes: [ {
          value: '平台目标',
          label: '平台目标'
        }, {
          value: '商家目标',
          label: '商家目标'
        }],
        periodTypes: [{
          value: '周目标',
          label: '周目标'
        }, {
          value: '月目标',
          label: '月目标'
        }, {
          value: '季目标',
          label: '季目标'
        }, {
          value: '年目标',
          label: '年目标'
        }],
        size: "small",
        filters: {
          targetType: "",
          indicatorName: "",
          time: "",
          res: {
            "total": 1,
            "code": "0000",
            "rows": [{
              "id":1,
              "targetType": "平台目标",
              "indicatorName": "成交金额",
              "periodType": "周目标",
              "time": "2019/12/23-2019/12/29",
              "targetValue":"1000"
            }]
          }
        },
        columns: [
          {prop: "id", label: "序号", minWidth: 50},
          {prop: "indicatorName", label: "指标名称", minWidth: 120},
          {prop: "targetType", label: "目标类型", minWidth: 120},
          {prop: "periodType", label: "周期类型", minWidth: 120},
          {prop: "targetValue", label: "目标值", minWidth: 50},
          {prop: "time", label: "有效时间", minWidth: 120}
        ],
        filterColumns: [],
        pageRequest: {pageNum: 1, pageSize: 10},
        pageResult: {},

        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        editDialogVisibleMatching: false,
        editLoading: false,
        dataFormRules: {
          replyContent: [{required: true, message: "回复内容", trigger: "blur"}]
        },
        // 新增编辑界面数据
        dataForm: {
          targetPercentage: "",
          channelName: "",
          startEventName: "",
          endEventName: "",
          time:""
        },
        dataFormMatching: {
          matching:""
        },
        shopList: [],
        statusList: [],
        checked: []
      };
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
        this.$refs.CyTable.findPageStaticState(this.filters);
      },
      // 显示新增界面
      handleAdd: function () {
        this.editDialogVisible = true;
        this.operation = true;
        this.resetForm("dataForm");
      },
      //选择框
      selectionChange: function (params) {
        this.checked = params.selections
      },
      // 批量删除
      handleDelete: function (data) {
        if (data != null && data.params != null && data.params.length > 0) {
          let ids = data.params.map(item => item.id).toString();

          var params = {};
          params.t = "changeProduct";
          params.ids = ids;
          var this_ = this;
          this_.$message({message: "操作成功", type: "success"});
          this_.findPage(null);
        }
      },
      // 显示编辑界面
      handleEdit: function (params) {
        this.editDialogVisible = true;
        this.operation = false;
        this.$nextTick(function () {
          this.dataForm = Object.assign({}, params.row);
          this.dataForm.time=[new Date(2019, 11, 23), new Date(2019, 11, 29)]
        });
      },
      // 编辑
      submitForm: function () {
        this.$message({message: "操作成功", type: "success"});
        this.editDialogVisible = false;
      },

      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property]);
      },

      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
          this.filters.start_time = "";
          this.filters.end_time = "";
          this.findPage(null);
        }
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        let temp = [];

        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
      //列表下载
      downloadExcel() {
        this.$confirm("确定下载列表文件?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let filename = "换货管理下载";
            this.$refs.CyTable.exportData(
              this.filters,
              this.filterColumns,
              filename
            );
          })
          .catch(() => {
          });
      },
      handleClick(tab, event) {
        if(tab.name =="1"){
          this.filters.res={
            "total": 1,
            "code": "0000",
            "rows": [{
              "id":1,
              "targetType": "平台目标",
              "indicatorName": "成交金额",
              "periodType": "周目标",
              "time": "2019/12/23-2019/12/29",
              "targetValue":"1000"
            }]
          }
        }else if(tab.name =="2"){
          this.filters.res={
            "total": 3,
            "code": "0000",
            "rows": [{
              "id":1,
              "targetType": "平台目标",
              "indicatorName": "成交金额",
              "periodType": "月目标",
              "time": "2019/12/23-2019/12/29",
              "targetValue":"1000"
            },
              {
                "id":2,
                "targetType": "平台目标",
                "indicatorName": "成交客户数",
                "periodType": "月目标",
                "time": "2019/12/23-2019/12/29",
                "targetValue":"1000"
              },
              {
                "id":3,
                "targetType": "平台目标",
                "indicatorName": "成交转化率",
                "periodType": "月目标",
                "time": "2019/12/23-2019/12/29",
                "targetValue":"1000"
              }]
          }
        }else if(tab.name =="3"){
          this.filters.res={
            "total": 4,
            "code": "0000",
            "rows": [{
              "id":1,
              "targetType": "平台目标",
              "indicatorName": "成交金额",
              "periodType": "季目标",
              "time": "2019/12/23-2019/12/29",
              "targetValue":"1000"
            },
              {
                "id":2,
                "targetType": "平台目标",
                "indicatorName": "成交金额",
                "periodType": "季目标",
                "time": "2019/12/23-2019/12/29",
                "targetValue":"1000"
              },
              {
                "id":3,
                "targetType": "平台目标",
                "indicatorName": "成交金额",
                "periodType": "季目标",
                "time": "2019/12/23-2019/12/29",
                "targetValue":"1000"
              },
              {
                "id":4,
                "targetType": "平台目标",
                "indicatorName": "成交金额",
                "periodType": "季目标",
                "time": "2019/12/23-2019/12/29",
                "targetValue":"1000"
              }]
          }

        }else {
          this.filters.res={
            "total": 2,
            "code": "0000",
            "rows": [{
              "id":1,
              "targetType": "平台目标",
              "indicatorName": "成交金额",
              "periodType": "年目标",
              "time": "2019/12/23-2019/12/29",
              "targetValue":"1000"
            },
              {
                "id":2,
                "targetType": "平台目标",
                "indicatorName": "成交金额",
                "periodType": "年目标",
                "time": "2019/12/23-2019/12/29",
                "targetValue":"1000"
              }]
          }
        }
        this.findPage(this.filters);
      }
    },
    mounted() {
      this.initColumns()
    }
  };
</script>

<style>
  .styleReplace .el-form-item__label{
    width:100px !important
  }
  .styleReplace .el-form-item__content{
    margin-left: 100px !important
  }
</style>
